//
//  M_PartIndexCell.swift
//  letuofu
//
//  Created by WH-JS on 2019/12/19.
//  Copyright © 2019 WH-JS. All rights reserved.
//

import UIKit
import SDWebImage

class M_PartIndexCell: UITableViewCell {
    let mainImg = UIImageView()
    let shareBtn = Button()
    let titleLab = UILabel()
    let subLab = UILabel()
    var celldata : [String : Any?]? {
        didSet{
            loadData()
        }
    }
    
    //定义一个闭包类型
    typealias shareBlock = () -> Void
    //声明一个变量
    var shareBtnBack: shareBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func callShareBlock(block: @escaping shareBlock) {
        shareBtnBack = block
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setupUI() {
        self.backgroundColor = .white
        
        //画横线
        let line = drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        drawLabel(label: titleLab,text: "当广东人来到动物园。。。", fontSize: 14, textColor: .x333333) { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(10)
        }
        
        drawButton(button: shareBtn, imageNamed: "fenxiang_90") { (make) in
            make.top.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-18)
            make.width.equalTo(18)
            make.height.equalTo(18)
        }
        shareBtn.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
        shareBtn.addTarget(self, action: #selector(sharebtn(btn:)), for: .touchUpInside)
        
        drawLabel(label: subLab,text: "小动物们在园里早已瑟瑟发抖...", fontSize: 12, textColor: .x999999) { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(33)
        }
        
        drawImage(imageView: mainImg, named: "paiduihuibanner_86") { (make) in
            make.top.equalTo(subLab.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
    
    func loadData() {
        titleLab.text = celldata?.getString("title")
        subLab.text = celldata?.getString("describe")
        let urlAry = celldata?.getString("imgs")?.components(separatedBy: ",")
        if let url = urlAry?.first {
            SDWebImageManager.shared.loadImage(with: URL(string: url), progress: nil, completed: {image,_,_,_,_,_ in
                mainQueueAsync {
                    self.redrawImg(img: image)
                }
            })
            
//            mainImg.sd_setImage(with: URL(string: url), completed: { (image,_,_,_) in
//
//                mainQueueAsync {
//                    self.redrawImg(img: image)
//                }
//            })
        }
    }
    
    func redrawImg(img: UIImage?) {
        //需要重新计算图片比例让CeLL自适应
        var hight = 0
        if img != nil {
            hight = Int(img!.size.height/img!.size.width*(375-20))
        }
        self.addSubview(mainImg)
        mainImg.image = img
        mainImg.snp_remakeConstraints({ (make) in
            make.top.equalTo(subLab.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(hight)
            make.bottom.equalToSuperview().offset(-10)
        })
        mainImg.layer.cornerRadius = 5
        mainImg.clipsToBounds = true
    }
    
    @objc func sharebtn(btn: Button) {
        if shareBtnBack != nil {
            shareBtnBack!()
        }
    }
}
